package com.codeanalysissys.backend.controller;

import com.codeanalysissys.backend.entity.response.ResponseCode;
import com.codeanalysissys.backend.entity.response.Result;
import com.codeanalysissys.backend.service.frame.SseEmitterService;
import com.codeanalysissys.backend.utils.CookieUtil;
import com.codeanalysissys.backend.utils.RedisCache;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sse")
public class SseEmitterController {
    @Autowired
    private SseEmitterService sseEmitterService;

    /**
     * 建立 sse 长连接
     *
     * @param clientId 客户端提供当前连接 Id
     * @return 新创建的 SseEmitter 实例
     */
    @CrossOrigin // 如果nginx做了跨域处理，此处可去掉
    @GetMapping("/create-sse-connect")
    public SseEmitter createSseConnect(@RequestParam("clientId") String clientId) {
        return sseEmitterService.createSseConnect(clientId);
    }

    /**
     * 关闭 sse 连接
     **/
    @GetMapping("/close-sse-connect")
    public Result closeSseConnect(@RequestParam("clientId") String clientId) {
        sseEmitterService.closeSseConnect(clientId);
        return new Result.Builder(ResponseCode.ANALYSIS_SUCCESS).build();
    }

}
